//! # 各类文件系统格式
//! 
//! 2021年1月31日 zg

use alloc::prelude::v1::*;
use tisu_fs::{DiskInfo, FileSystem, Leaf};

use crate::{filesystem::require::DoDirectory, virtio::disk_cache::get_cache};

use super::require::DoInfo;

pub mod fat32;
pub mod tianmu;

impl tisu_fs::Format for fat32::FATManger {
    fn to_system(&self)->tisu_fs::FileSystem {
        let t;
        unsafe {
            t = &mut *(self as *const Self as *mut Self);
        }
        FileSystem::new(get_cache(), t, 0)
    }

    fn parse_node(&self, block_idx : usize)->Result<Vec<Leaf>, ()> {
        let mut rt = Vec::new();
        let sub_dir = block_idx;
        unsafe {
            let t = self as *const Self as *mut Self;
            let t = (*t).get_directory(sub_dir).unwrap();
            for mut item in t.items {
                if item.start_cluster == 0 {
                    item.start_cluster = 2;
                }
                rt.push(item.to_leaf());
            }
            Ok(rt)
        }
    }

    fn get_block_chain(&self, start_idx : usize)->Result<Vec<usize>, ()> {
        unsafe {
            let t = self as *const Self as *mut Self;
            Ok((*t).get_all_cluster(start_idx).unwrap())
        }
    }

    fn parse_super_block(&self)->DiskInfo {
        DiskInfo {
            stype: tisu_fs::SystemType::FAT32,
            total_size: self.get_total_size(),
            block_size: self.cluster_size,
            root_directory_block_idx: 2,
            block_start_addr: self.cluster_start_addr,
        }
    }
}